package com.meego.common.timer;

import java.util.Calendar;
import java.util.Date;

public class DailyEvent extends TimedAdaptor {

    private Date mDate;
    private boolean mSingleShot;

    public DailyEvent(int hour, int min, int sec, boolean singleShot) {
        this.mSingleShot = singleShot;

        Date now = new Date();

        Calendar cal = Calendar.getInstance();

        cal.setTime(now);
        cal.set(11, hour);
        cal.set(12, min);
        cal.set(13, sec);
        cal.set(14, 0);

        this.mDate = cal.getTime();

        if (now.compareTo(this.mDate) > 0) {
            cal.add(5, 1);
            this.mDate = cal.getTime();
        }
    }

    @Override
    public Date getNextEvent() {
        Date ret = this.mDate;

        if (this.mSingleShot) {
            this.mDate = null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(this.mDate);
            cal.add(5, 1);
            this.mDate = cal.getTime();
        }

        return ret;
    }
}
